package com.goldsign.pay.provider.accountbase.service.impl;

import com.goldsign.pay.common.exception.GcServiceException;
import com.goldsign.pay.common.util.Constant;
import com.goldsign.pay.common.util.Converter;
import com.goldsign.pay.provider.accountbase.vo.WaterNoLogDO;
import com.goldsign.pay.provider.accountbase.dao.CommonAccountDao;
import com.goldsign.pay.provider.accountbase.service.CommonService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.sql.SQLException;

@Service
public class CommonServiceImpl implements CommonService{
	
	@Autowired
	private CommonAccountDao commonDao;
	
	@Override
	public String execGetWaterNo(String tableName, String columnName) throws GcServiceException {
		try{
			WaterNoLogDO waterNoLogDO = new WaterNoLogDO();
			String id="00000000000000000001";
			waterNoLogDO.setColumn_name(columnName);
			waterNoLogDO.setTable_name(tableName);
			WaterNoLogDO sequence = commonDao.getWaterNo(waterNoLogDO);
			if (sequence == null) {
				waterNoLogDO.setWater_no(id);
				commonDao.saveWaterNo(waterNoLogDO);
			} else {
				id = (Integer.parseInt(((String) sequence.getWater_no()).substring(3)) + 1) + "";
				id = Converter.leftPad(id, 20, '0');
			}
			//更新表中流水号
			waterNoLogDO.setWater_no(id);
			commonDao.updateWaterNo(waterNoLogDO);
			return id;
		}catch(SQLException e){
			
			throw new GcServiceException("DATABASE_ERROR", Constant.DATABASE_ERROR);
		}
	}

}
